package xy.web;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public final class HtmServlet extends HttpServlet {

	private static final long serialVersionUID = -7962065549698615440L;
	
	private String uriTail = ".htm";
	
	protected void service(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		String uri = req.getRequestURI();
		
		if(!uri.endsWith(uriTail)){
			throw new ServletException("Invalid URI");
		}
		
		uri = uri.substring(0, uri.length() - uriTail.length());
		
		String[] as = uri.split("/");
		
		StringBuffer param = new StringBuffer();
		
		for(int i=as.length-1; i>=0; i--){
			int p = as[i].indexOf("-");
			if(p!=-1){
				String name = as[i].substring(0, p);
				String val = as[i].substring(p+1);
				param.append("&")
				.append(name).append("=").append(val);
			}else{
				param.insert(0, "/"+as[i]+".do?t"+(new Date()).getTime()+"=1");
				break;
			}
		}
		
		
		req.getRequestDispatcher(param.toString()).forward(req, res);
	}

}
